package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.SearchHistoryHolder;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.raizlabs.android.dbflow.list.FlowQueryList;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import chihane.jdaddressselector.model.HistorySearch;
import chihane.jdaddressselector.model.HistorySearch_Table;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：数据搜索
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/5 10:30
 * 修改备注：
 * ================================================
 */

public class DataSearchActivity extends BaseActivity {

    @Bind(R.id.et_search)
    EditTextWithClear etSearch; // 搜索框
    @Bind(R.id.tv_cancel)
    TextView tvCancel; // 取消
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    @Bind(R.id.tv_clear_history)
    TextView tvClearHistory;

    public static final int RESULT_CODE_DATA_SEARCH = 1001;

    private RecyclerArrayAdapter adapter;

    private FlowQueryList<HistorySearch> historySearches;

    private HistorySearch historyKeyword;

    @Override
    public void setLayout() {
        hideActionBar();
        setContentLayout(R.layout.activity_data_search);
    }

    @Override
    public void initMember() {

        etSearch.setInputType(EditorInfo.TYPE_CLASS_TEXT); //软键盘 默认中文
//        etSearch.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); // 默认英文
//        etSearch.setHint(DecimalFormatUtil.getInstance().formats("123456.126"));
        initRecyclerView();
        iniData();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 15, 15));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new SearchHistoryHolder(parent);
            }
        });
    }

    private void iniData() {

        historySearches = SQLite.select(HistorySearch_Table.keyWord)
                .distinct()
                .from(HistorySearch.class)
                .where(HistorySearch_Table.userId.eq(UserXML.getUserId(DataSearchActivity.this)))
                .orderBy(HistorySearch_Table.id, false)
                .flowQueryList();

        adapter.addAll(new ArrayList<>(historySearches));
    }

    @Override
    public void addListener() {

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int keyCode, KeyEvent keyEvent) {
                if (keyCode == EditorInfo.IME_ACTION_SEARCH) {
                    String keyWord = StringUtil.getEditText(etSearch);
                    if (!StringUtil.isStrEmpty(keyWord)) {
                        insertKeyWord(keyWord);
                        setResultString(keyWord);
                    }
                    return true;
                }
                return false;
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                historyKeyword = (HistorySearch) adapter.getAllData().get(position);
                setResultString(historyKeyword.getKeyWord());
            }
        });

    }

    /**
     * 将关键字插入数据库
     */
    private void insertKeyWord(String keyWord) {
        SQLite.insert(HistorySearch.class)
                .columns(HistorySearch_Table.userId, HistorySearch_Table.keyWord)
                .values(UserXML.getUserId(DataSearchActivity.this), keyWord)

                .execute();
    }

    /**
     * 返回关键字
     *
     * @param keyWord
     */
    private void setResultString(String keyWord) {
        Intent intent = new Intent();
        intent.putExtra("KeyWord", keyWord);
        setResult(RESULT_CODE_DATA_SEARCH, intent);
        finish();
    }

    @Override
    public void setHandler() {
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @OnClick(R.id.tv_clear_history)
    public void onClick() {
        Delete.table(HistorySearch.class);
        adapter.removeAll();
    }
}
